package com.miniapp.controller;

import com.miniapp.annotation.SysLogAnno;
import com.miniapp.dto.PageDTO;
import com.miniapp.dto.SimpleSearchDTO;
import com.miniapp.dto.form.PasswordDTO;
import com.miniapp.entity.MiniUserEntity;
import com.miniapp.service.MiniUserService;
import com.miniapp.utils.BaseUtils;
import com.miniapp.utils.R;
import com.miniapp.validator.Assert;
import com.miniapp.validator.ValidatorUtils;
import com.miniapp.validator.group.AddGroup;
import com.miniapp.validator.group.UpdateGroup;
import org.apache.commons.lang.ArrayUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;


/**
 * 系统用户
 * 
 * @author linyuchi
 * @email xunli-03@163.com
 * @date 2016年10月31日 上午10:40:10
 */
@RestController
@RequestMapping("/miniapp/user")
public class MiniUserController extends AbstractController {
	@Autowired
	private MiniUserService miniUserService;

	/**
	 * 所有用户列表
	 */
	@GetMapping("/list")
	public R list(){
		SimpleSearchDTO simpleSearchDTO = getSearchDtoFromJqGrid();
		simpleSearchDTO.setSqlSelect(new String[]{"id","username","name","mobile","create_time","status","balance"});
		PageDTO<MiniUserEntity> page = miniUserService.findPageBySimpleSearch(simpleSearchDTO);
		return R.ok().put("page", page);

	}

	/**
	 * 获取登录的用户信息
	 */
	@GetMapping("/info")
	public R info(){

		MiniUserEntity user = miniUserService.getById(this.getUserId());
		return R.ok().put("user", user);

	}
	
	/**
	 * 修改登录用户密码
	 */
	@SysLogAnno("修改密码")
	@PostMapping("/password")
	public R password(@RequestBody PasswordDTO form){

		Assert.isBlank(form.getNewPassword(), "新密码不为能空");
		Assert.isBlank(form.getPassword(), "旧密码不为能空");
		//sha256加密
		String password = new Sha256Hash(form.getPassword(), getUser().getSalt()).toHex();
		//sha256加密
		String newPassword = new Sha256Hash(form.getNewPassword(), getUser().getSalt()).toHex();
				
		//更新密码
		boolean flag = miniUserService.updatePassword(getUserId(), password, newPassword);
		if(!flag){
			return R.error("原密码不正确");
		}
		
		return R.ok();
	}
	/**
	 * 修改登录用户
	 */
	@SysLogAnno("修改密码")
	@PostMapping("/name")
	public R name(@RequestBody  PasswordDTO passwordDTO){

		if (BaseUtils.isEmpty(passwordDTO.getName())){
			return R.error("用户名为空");
		}
		MiniUserEntity user = miniUserService.getById(this.getUserId());
		user.setName(passwordDTO.getName());
		miniUserService.save(user);
		return R.ok();
	}

	/**
	 * 用户信息
	 */
	@GetMapping("/info/{userId}")
	public R info(@PathVariable("userId") Long userId){

		MiniUserEntity user = miniUserService.getById(userId);
		return R.ok().put("user",  user);
	}
	
	/**
	 * 保存用户
	 */
	@SysLogAnno("保存用户")
	@PostMapping("/save")
	public R save(@RequestBody MiniUserEntity user){
		ValidatorUtils.validateEntity(user, AddGroup.class);

//		校验用户名
		if(BaseUtils.isNotEmpty(miniUserService.findByUserName(user.getUsername()))){
			return R.error("用户名已存在");
		}
		miniUserService.saveUser(user);
		return R.ok();
	}
	/**
	 * 修改余额
	 */
	@SysLogAnno("保存用户")
	@PostMapping("/add_money")
	public R addMoney(@RequestBody MiniUserEntity user){

		return miniUserService.updateBalance(getUserId(),user.getId(),user.getBalance());

	}
	/**
	 * 修改用户折扣
	 */
	@SysLogAnno("修改用户折扣")
	@PostMapping("/update_rebate")
	public R updateRebate(@RequestBody MiniUserEntity user){

		MiniUserEntity entity = miniUserService.getById(user.getId());

		BigDecimal rebate = BaseUtils.formatBigDecimalM2(user.getRebate());
		entity.setRebate(rebate);
		miniUserService.updateById(entity);
		return R.ok();
	}
	/**
	 * 修改用户
	 */
	@SysLogAnno("修改用户")
	@PostMapping("/update")
	@RequiresPermissions("sys:user:update")
	public R update(@RequestBody MiniUserEntity user){
		ValidatorUtils.validateEntity(user, UpdateGroup.class);

		user.setCreateUserId(getUser().getId());
		miniUserService.saveUser(user);
		
		return R.ok();
	}
	
	/**
	 * 删除用户
	 */
	@SysLogAnno("删除用户")
	@PostMapping("/delete")
	public R delete(@RequestBody Long[] userIds){
		if(ArrayUtils.contains(userIds, 1L)){
			return R.error("系统管理员不能删除");
		}
		
		if(ArrayUtils.contains(userIds, getUserId())){
			return R.error("当前用户不能删除");
		}
		for (Long userId :userIds){
			miniUserService.removeById(userId);
		}
		return R.ok();
	}






}
